//
//  CMScrollPageView.m
//  MoviesStore 2.0
//
//  Created by 周筱雯 on 15/6/4.
//  Copyright (c) 2015年 caimiao. All rights reserved.
//

#import "CMScrollPageView.h"

#define SM_DEVICE_WIDTH       self.frame.size.width
#define SM_DEVICE_HEIGHT       self.frame.size.height
#define SM_BUTTON_HEIGHT 45.0

@implementation CMScrollPageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CMNeedUseDelegate = YES;
        
        self.backgroundColor = [UIColor clearColor];
        
        if (self.CMScrollView == nil) {
            self.CMScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,  self.width,  self.height)];
            
            self.CMScrollView.pagingEnabled = YES;
            self.CMScrollView.delegate = self;
            self.CMScrollView.showsHorizontalScrollIndicator = NO;
            self.isShowOfLine = NO;
            self.isShowOfMoveView = NO;
            self.colorOfContent = DEF_COLOR_MAINCOLOR;
            self.colorOfGreyLine = DEF_COLOR_MAINCOLOR;
            self.colorOfheaderView = [UIColor whiteColor];
        }
        [self addSubview:self.CMScrollView];
    }
    return self;
}

-(void)setZDYContentOfView:(NSArray *)CMViewArray {
    
    CGFloat Y = 0;
    
    if (self.items.count>0) {
        Y = SM_BUTTON_HEIGHT;
    }
    
    for (int i = 0; i < [CMViewArray count]; i++) {
        
        UIView * view = (UIView *) CMViewArray [i] ;
        
        view.tag =999;
        
        view.frame = CGRectMake(self.frame.size.width * i, Y, view.width, view.height);
        
        [self.CMScrollView addSubview:view];
        
        
    }
    [self.CMScrollView setContentSize:CGSizeMake(self.width * [CMViewArray count], 0)];
}


//自定义frame + 移除
-(void)setZDYRemoveContentOfView:(NSArray *)CMViewArray {
    
    for (UIView * view in self.subviews) {
        
        if (view.tag == 999) {
            
            [view removeFromSuperview];
        }
    }
    
    CGFloat Y = 0;
    
    if (self.items.count>0) {
        Y = SM_BUTTON_HEIGHT;
    }
    
    for (int i = 0; i < [CMViewArray count]; i++) {
        
        UIView * view = (UIView *) CMViewArray [i] ;
        
        view.tag =999;
        
        view.frame = CGRectMake(self.frame.size.width * i, Y, view.width, view.height);
        
        [self.CMScrollView addSubview:view];
        
        
    }
    [self.CMScrollView setContentSize:CGSizeMake(self.width * [CMViewArray count], 0)];
}

#pragma mark 添加ScrollowViewd的ContentView

-(void)setContentOfView:(NSArray *)CMViewArray{
    
    CGFloat Y = 0;
    
    if (self.items.count>0) {
        Y = SM_BUTTON_HEIGHT;
    }
    
    for (int i = 0; i < [CMViewArray count]; i++) {
        
        [CMViewArray[i] setFrame:CGRectMake( self.width * i, Y,  self.width, self.height - Y)];
        
        [self.CMScrollView addSubview:CMViewArray[i]];
    }
    [self.CMScrollView setContentSize:CGSizeMake(self.width * [CMViewArray count], 0)];
}


-(void)setItems:(NSString *)firstItem otherItems:(NSString *)otherItem, ...{
    
    va_list args ;
    va_start(args, otherItem);
    
    self.items = [[NSMutableArray alloc] init];
    [self.items addObject:firstItem];
    
    for (NSString *str = otherItem; str != nil; str = va_arg(args,NSString*)) {
        
        [self.items addObject:str];
    }
    va_end(args);
    
    [self setSegmentControl];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    CMNeedUseDelegate = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = (self.CMScrollView.contentOffset.x + SM_DEVICE_WIDTH /2.0) / SM_DEVICE_WIDTH;
    if (CMCurrentPage == page) {
        return;
    }
    CMCurrentPage= page;
    self.selectedPageIndex = CMCurrentPage;
    [self selectedSegmentIndex:CMCurrentPage];
}

#pragma mark 移动ScrollView到某个页面
-(void)moveScrollowViewAthIndex:(NSInteger)aIndex{
    
    CMNeedUseDelegate = NO;
    self.CMScrollView.contentOffset = CGPointMake(self.width * aIndex, 0);
    CMCurrentPage= aIndex;
    self.selectedPageIndex = CMCurrentPage;
    [self selectedSegmentIndex:CMCurrentPage];
}


#pragma mark 按钮设置
-(void)setSegmentControl{
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame),SM_BUTTON_HEIGHT)];
    self.headerView.backgroundColor = self.colorOfheaderView;
    [self addSubview:self.headerView ];
    
    
    NSInteger segmentWidth = self.width/[self.items count];
    
    self.buttonArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i<[self.items count];i++) {
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*(SM_DEVICE_WIDTH/[self.items count]), 0, segmentWidth, SM_BUTTON_HEIGHT)];
        
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [button setTitle:self.items[i] forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:self.colorOfContent forState:UIControlStateNormal];
            self.recordButton = button;
        }
        
        button.tag = i;
        
        [button addTarget:self action:@selector(segementButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttonArray addObject:button];
        
        [self addSubview:button];
        
        
        self.centerGreyLineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(button.frame)-1, SM_BUTTON_HEIGHT/4, 1, SM_BUTTON_HEIGHT/2)];
        self.centerGreyLineView.hidden = self.isShowOfLine;
        self.centerGreyLineView.backgroundColor = DEF_COLOR_F0F0F0;
        [button addSubview:self.centerGreyLineView];
    }
    
    //移动标识
    self.infoMoveView = [[UIView alloc]initWithFrame:CGRectMake(0, self.headerView.height - 2, self.recordButton.width, 2)];
    self.infoMoveView.hidden = self.isShowOfMoveView;
    self.infoMoveView.backgroundColor = self.colorOfContent;
    [self.headerView addSubview:self.infoMoveView];
    
    //上边距线
    [MXUtility topLayerborder:self.headerView borderColor:DEF_COLOR_F0F0F0];
    [MXUtility bottomLayerborder:self.headerView borderColor:DEF_COLOR_F0F0F0];//下
}
#pragma mark 点击事件

-(void)segementButtonClick:(id)sender{
    
    UIButton *btn = (UIButton *)sender;
    
    //scroll移动
    
    [self moveScrollowViewAthIndex:btn.tag];
    
    self.selectedPageIndex = btn.tag;
    
    [self changeSelectButton:btn];
}

-(void)changeSelectButton:(UIButton *)btn{
    
    [self.recordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.buttonArray removeObjectAtIndex:self.recordButton.tag];
    [self.buttonArray insertObject:self.recordButton atIndex:self.recordButton.tag];
    
    [btn setTitleColor:self.colorOfContent forState:UIControlStateNormal];
    
    self.recordButton = btn;
    
    [self.buttonArray removeObjectAtIndex:btn.tag];
    [self.buttonArray insertObject:self.recordButton atIndex:btn.tag];
    
    [self moveframe:btn.frame.origin.x];
}

#pragma mark 设置选中按钮
-(void)selectedSegmentIndex:(NSInteger)index{
    if (index>=[self.items count]) {
        return;
    }
    
    UIButton *btn = self.buttonArray[index];
    
    [self changeSelectButton:btn];
}


-(void)moveframe:(CGFloat)width{
    
    [UIView animateWithDuration:0.35 animations:^{
        CGRect frame = _infoMoveView.frame;
        frame.origin.x = width;
        _infoMoveView.frame = frame;
    }];
}

@end
